<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>图片转base64</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        color-scheme: light dark;
      }
      html,
      body {
        width: 100%;
        height: 100%;
      }
      main {
        max-width: 1200px;
        min-height: 600px;
        height: 100%;
        margin: 0 auto;
      }
      #showText {
        width: 100%;
        height: 600px;
        padding: 6px;
        color: #cfd3dc;
      }
      #operation {
        width: 100%;
        height: 100px;
        padding: 30px 0;
        display: flex;
        align-items: center;
        justify-content: flex-start;
      }
      button {
        margin-right: 10px;
        padding: 4px 8px;
      }
      img {
        height: 100px;
        object-fit: cover;
        margin-left: 10px;
      }
      .mask {
        position: absolute;
        top: 0;
        left: 0;
        z-index: 100;
        width: 100%;
        height: 100%;
        background-color: rgba(255, 255, 255, 0.1);
        display: none;
        justify-content: center;
        align-items: center;
        pointer-events: none;
      }

      body.active .mask {
        display: flex;
      }
      body.active main {
        opacity: 0;
      }
    </style>
  </head>
  <body>
    <main>
      <div id="operation">
        <button onclick="getBase64()">选择图片</button>
        <button onclick="copy2()">复制文本</button>
        <img alt="" />
      </div>
      <textarea id="showText"></textarea>
    </main>
    <div class="mask">
      <h1>请将图片拖到这里</h1>
    </div>

    <script>
      function copy(text) {
        //复制函数（text为需要复制的字符串）
        const input = document.createElement('input')
        input.style.cssText = 'position: absolute;top: -100000px;opacity: 0;'
        document.body.appendChild(input)
        input.value = text
        input.select()
        input.setSelectionRange(0, text.length)

        document.execCommand('copy')

        document.body.removeChild(input)
      }

      function copy2() {
        const text = document.getElementById('showText').value
        if (text) {
          copy(text)
        } else {
          alert('没有内容')
        }
      }

      // 展示
      function show(base64) {
        document.getElementById('showText').value = base64

        document.querySelector('img').src = base64
      }

      // 获取base64函数
      function getBase64() {
        const fileInput = document.createElement('input') // 创建input元素
        fileInput.type = 'file' // 设置元素的类型
        fileInput.accept = 'image/*'
        fileInput.click() // 执行元素的点击事件
        fileInput.oninput = (event) => {
          const fileList = event.target.files
          if (fileList) {
            toBase64(fileList[0], show)
          }
        }
      }

      // 转base64
      function toBase64(file, fn) {
        const reader = new FileReader()
        reader.readAsDataURL(file)
        reader.onload = () => {
          fn && fn(reader.result)
        }
      }

      // 文件拖入
      document.body.addEventListener('dragover', (event) => {
        // 阻止默认的浏览器行为
        event.preventDefault()
        document.body.classList.add('active')
      })

      // 文件拖出
      document.body.addEventListener('dragleave', () => {
        document.body.classList.remove('active')
      })

      // 获取拖入的文件并处理
      document.body.addEventListener('drop', (event) => {
        event.preventDefault()

        // 获取拖入的文件列表
        const fileList = event.dataTransfer.files

        if (fileList) {
          const file = fileList[0]

          if (/^image\/.*/.test(file.type)) {
            toBase64(file, show)
          } else {
            alert('不是图片：' + file.name)
          }
        }

        document.body.classList.remove('active')
      })
    </script>
  </body>
</html>
